package com.web.base.auth.service;

import com.blue.jdbc.JdbcDataSource;
import com.web.base.auth.dao.MenuButtonDao;
import com.web.base.auth.dao.MenuDao;
import com.web.base.auth.dao.RoleMenuButtonDao;
import com.web.base.auth.exception.MenuButtonException;
import com.web.base.auth.model.MenuButton;
import com.web.base.auth.model.RoleMenuButton;
import com.web.core.util.Const;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
@Service
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class MenuButtonService
{
	@Resource
	private MenuButtonDao menuButtonDao;
	@Resource
	private MenuDao menuDao;
	@Resource
	private RoleMenuButtonDao roleMenuButtonDao;

	public MenuButtonService()
	{
	}
	
	public MenuButton save(MenuButton menuButton)
	{
		if (menuButtonDao.exist(menuButton, "code", "appId"))
			throw new MenuButtonException("代码已经存在：" + menuButton.getCode());

		if (menuButton.getId() == null)
		{
			menuButtonDao.save(menuButton);
			menuDao.inc(menuButton.getMenuId(), "buttons", 1);
		}
		else
		{
			menuButtonDao.update(menuButton);
		}
		MenuButton a = menuButtonDao.get(menuButton.getId());
		return a;
	}
	
	public MenuButton get(Integer id)
	{
		MenuButton log = menuButtonDao.get(id);
		return log;
	}
	
	public List<MenuButton> list(MenuButton menuButton)
	{
		List<MenuButton> list = menuButtonDao.list(menuButton);
		return list;
	}
	
	public void delete(MenuButton menuButton)
	{
		roleMenuButtonDao.deleteBy("buttonId", menuButton.getId());
		menuButtonDao.delete(menuButton);
		menuDao.inc(menuButton.getMenuId(), "buttons", -1);
	}
	
	public List<RoleMenuButton> listRoleMenuButton(RoleMenuButton roleMenuButton)
	{
		List<RoleMenuButton> list = roleMenuButtonDao.list(roleMenuButton);
		return list;
	}
	
	public List<MenuButton> listSelect(Integer roleId, Integer appId)
	{
		List<MenuButton> list = roleMenuButtonDao.listSelect(roleId, appId);
		return list;
	}
	
}
